package jing.upnp.ssdp.type.usn;

import android.util.Log;

/**
 * Created by ouyang on 15-9-10.
 */
public class UriFactory {

    private static final String TAG = "UriFactory";

    private UriFactory() {
    }

    public static Uri createWithString(String string) {
        Uri uri = new Uri();

        do {
            if (string == null) {
                Log.d(TAG, "uri is null");
                uri = null;
                break;
            }

            if (string.equals(Uri.URI_ROOT_DEVICE)) {
                uri.setType(Uri.Type.RootDevice);
                break;
            }

            String[] a = string.split(":");
            if (a.length == 5) {
                if (a[0].equals(Uri.URI_URN)) {
                    if (a[1].equals(Uri.URI_UPNP)) {
                        if (a[2].equals(Uri.URI_DEVICE)) {
                            uri.setType(Uri.Type.UPnPDevice);
                            uri.setDeviceType(a[3]);
                            uri.setDeviceVersion(a[4]);
                            break;
                        } else if (a[2].equals(Uri.URI_SERVICE)) {
                            uri.setType(Uri.Type.UPnPService);
                            uri.setServiceType(a[3]);
                            uri.setServiceVersion(a[4]);
                            break;
                        }
                    }
                    else {
                        uri.setDomainName(a[1]);

                        if (a[2].equals(Uri.URI_DEVICE)) {
                            uri.setType(Uri.Type.NonUPnPDevice);
                            uri.setDeviceType(a[3]);
                            uri.setDeviceVersion(a[4]);
                            break;
                        } else if (a[2].equals(Uri.URI_SERVICE)) {
                            uri.setType(Uri.Type.NonUPnPService);
                            uri.setServiceType(a[3]);
                            uri.setServiceVersion(a[4]);
                            break;
                        }
                        else {
                            Log.d(TAG, "uri is not Device or Service: " + a[2]);
                            uri = null;
                            break;
                        }
                    }
                }
            }
            else if (a.length == 2) {
                if (a[0].equals("uuid")) {
                    uri.setType(Uri.Type.DeviceUUID);
                    uri.setUuid(a[1]);
                    break;
                }
            }
            else {
                Log.d(TAG, "uri invalid: " + string);
                uri = null;
                break;
            }
        } while (false);

        return uri;
    }

    public static Uri createRootDevice() {
        Uri uri = new Uri();
        uri.setType(Uri.Type.RootDevice);
        return uri;
    }

    public static Uri createDeviceUUID(String uuid) {
        Uri uri = new Uri();
        uri.setType(Uri.Type.DeviceUUID);
        uri.setUuid(uuid);
        return uri;
    }

    public static Uri createWithUPnPDevice(String deviceType, String deviceVersion) {
        Uri uri = new Uri();
        uri.setType(Uri.Type.UPnPDevice);
        uri.setDeviceType(deviceType);
        uri.setDeviceVersion(deviceVersion);
        return uri;
    }

    public static Uri createWithUPnPService(String serviceType, String serviceVersion) {
        Uri uri = new Uri();
        uri.setType(Uri.Type.UPnPService);
        uri.setServiceType(serviceType);
        uri.setServiceVersion(serviceVersion);
        return uri;
    }
}